package com.example.dataGroup;


import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.ace.ability.AceInternalAbility;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.MessageOption;
import ohos.rpc.MessageParcel;
import ohos.utils.net.Uri;

public class dataGroupConnect extends AceInternalAbility {
    private static final String BUNDLE_NAME = "com.example.dataGroupConnect";
    private static final String ABILITY_NAME = "dataConnectTest";
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "MainAbilitySlice");

    private static final String BASE_URI = "dataability:///com.example.dataGroup.PersonDataAbility";

    private static final String DATA_PATH = "/person";

    private static final String DB_COLUMN_PERSON_ID = "id";

    private static final String DB_COLUMN_NAME = "name";

    private static final String DB_COLUMN_GENDER = "gender";

    private static final String DB_COLUMN_AGE = "age";

    private static final int DB_INSERT=1001;

    private static final int DB_UPDATE=1002;

    private static final int DB_DELETE=1003;

    private static final int DB_SELECT=1004;

    private static final int DB_CREATE=1005;

    private DataAbilityHelper databaseHelper;

    private static dataGroupConnect instance;


    public dataGroupConnect() {
        super(BUNDLE_NAME, ABILITY_NAME);
    }

    public static dataGroupConnect getInstance() {
        if (instance == null) {
            synchronized (dataGroupConnect.class) {
                if (instance == null) {
                    instance = new dataGroupConnect();
                }
            }
        }
        return instance;
    }

    /*
    Ace注册
     */
    public void register() {
        this.setInternalAbilityHandler(this::onRemoteRequest);
        HiLog.error(LABEL_LOG, "connect started");
    }

    /*
    Ace取消注册
     */
    public void deregister() {
        this.setInternalAbilityHandler(null);
        HiLog.error(LABEL_LOG, "11");
    }

    private boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) {
        switch (code) {
            case DB_INSERT:
                break;
            case DB_UPDATE:
                break;
            case DB_DELETE:
                break;
            case DB_SELECT:
                break;
            case DB_CREATE:
                break;
            default:
        }

        return true;
    }

    private void query() {
        String[] columns = new String[] {DB_COLUMN_PERSON_ID,
                DB_COLUMN_NAME, DB_COLUMN_GENDER, DB_COLUMN_AGE};
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.between(DB_COLUMN_AGE, 15, 40);
        try {
            ResultSet resultSet = databaseHelper.query(Uri.parse(BASE_URI + DATA_PATH),
                    columns, predicates);
            if (resultSet == null || resultSet.getRowCount() == 0) {
                HiLog.info(LABEL_LOG, "query: resultSet is null or no result found");
                return;
            }
            resultSet.goToFirstRow();
            do {
                int id = resultSet.getInt(resultSet.getColumnIndexForName(DB_COLUMN_PERSON_ID));
                String name = resultSet.getString(resultSet.getColumnIndexForName(DB_COLUMN_NAME));
                String gender = resultSet.getString(resultSet.getColumnIndexForName(DB_COLUMN_GENDER));
                int age = resultSet.getInt(resultSet.getColumnIndexForName(DB_COLUMN_AGE));
                HiLog.info(LABEL_LOG, "query: Id :" + id + " Name :" + name + " Gender :" + gender + " Age :" + age);
            } while (resultSet.goToNextRow());
        } catch (DataAbilityRemoteException | IllegalStateException exception) {
            HiLog.error(LABEL_LOG, "query: dataRemote exception | illegalStateException");
        }
    }

    private void insert(int id, String name, String gender, int age) {
        ValuesBucket valuesBucket = new ValuesBucket();
        valuesBucket.putInteger(DB_COLUMN_PERSON_ID, id);
        valuesBucket.putString(DB_COLUMN_NAME, name);
        valuesBucket.putString(DB_COLUMN_GENDER, gender);
        valuesBucket.putInteger(DB_COLUMN_AGE, age);
        try {
            if (databaseHelper.insert(Uri.parse(BASE_URI + DATA_PATH), valuesBucket) != -1) {
                HiLog.info(LABEL_LOG, "insert successful");
            }
        } catch (DataAbilityRemoteException | IllegalStateException exception) {
            HiLog.error(LABEL_LOG, "insert: dataRemote exception|illegalStateException");
        }
    }

    private void update() {
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo(DB_COLUMN_PERSON_ID, 102);
        ValuesBucket valuesBucket = new ValuesBucket();
        valuesBucket.putString(DB_COLUMN_NAME, "ZhangSanPlus");
        valuesBucket.putInteger(DB_COLUMN_AGE, 28);
        try {
            if (databaseHelper.update(Uri.parse(BASE_URI + DATA_PATH), valuesBucket, predicates) != -1) {
                HiLog.info(LABEL_LOG, "update successful");
            }
        } catch (DataAbilityRemoteException | IllegalStateException exception) {
            HiLog.error(LABEL_LOG, "update: dataRemote exception | illegalStateException");
        }
    }

    private void delete() {
        DataAbilityPredicates predicates = new DataAbilityPredicates()
                .equalTo(DB_COLUMN_PERSON_ID, 100);
        try {
            if (databaseHelper.delete(Uri.parse(BASE_URI + DATA_PATH), predicates) != -1) {
                HiLog.info(LABEL_LOG, "delete successful");
            }
        } catch (DataAbilityRemoteException | IllegalStateException exception) {
            HiLog.error(LABEL_LOG, "delete: dataRemote exception | illegalStateException");
        }
    }
}
